<template>
  <div id="app">
    <div class="main">
      <router-view v-if="isFinished" />
    </div>
    <footer-nav v-if="this.$route.meta.showFooter"></footer-nav>
  </div>
</template>

<script>
import { POST } from '@/api/common'
import footerNav from '@/components/common/footerNav'
export default {
  name: 'App',
  data() {
    return {
      isFinished: false
    }
  },
  components: {
    footerNav
  },
  created() {
    if (this.$route.name == 'login') {
      this.isFinished = true
    } else {
      this.autoLogin() 
    }
  },
  methods: {
    autoLogin() {
      // 获取本地用户信息跟token
      let userInfo = this.$getStorage('userInfo')
      let token = this.$getToken()

      // 如果有token就自动登录
      if (token) {

        this.$loading('登录中...')
 
        let params = {
          tel: userInfo.account,
          password: userInfo.password
        }
        
        POST('api/login/', params).then(res => {
          // 自动登录完成才加载组件
          this.isFinished = true
          this.$close()
    
          if (res.code == 200) {
            document.getElementsByTagName('body')[0].style.backgroundColor = '#fbfbfb'
            this.$setToken(res.data.token)
            this.$store.state.user = res.data
            this.$router.push('/')
          }
          else if (res.code == 401) {
            this.$alert('登录过期！')
          }
          else {
            this.$alert('登录失败！')
            this.$router.push('/login')
          }
        })
      } 
      // 无token直接到登录页面
      else {
        this.$router.push('/login')
      }
    }
  }
}
</script>

<style>
  body {
    background-color: #fbfbfb;
  }
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #333;
    font-size: 14px;
  }
  .main {
    padding-bottom: 55px;
  }
</style>
